package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

//    /**
//     * 根据id查询套餐
//     * @param id
//     * @return
//     */
//    @GetMapping("/list")
//    public R<SetmealDto> selectById( String id , String status){
//        SetmealDto setmealDto = setmealService.getSetmealDto(id ,status);
//        return  R.success(setmealDto);
//    }

    /**
     * 套餐列表
     * @param categoryId
     * @return
     */

    @GetMapping("/list")
    public R<List<SetmealDto>> dishPulldown(String categoryId, String status){
        log.info("------下拉分类----");
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId)
                .eq(Setmeal::getStatus,status);
        List<Setmeal> setmeals = setmealService.list(queryWrapper);
        //新建一个Dto类 并将查询结果传入;
        List<SetmealDto> setmalDtos = new ArrayList<>();
        for (int i = 0; i < setmeals.size(); i++) {
            Setmeal setmeal = setmeals.get(i);
            //找到当前setmeal_dish对应的id
            LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper1);

            SetmealDto setmealDto = new SetmealDto();
            //复制到新的dto类
            BeanUtils.copyProperties(setmeal,setmealDto);
          //  BeanUtils.copyProperties(setmealDishList,setmealDto.getFlavors());
            setmealDto.setFlavors(setmealDishList);
            setmalDtos.add(setmealDto);
        }
        return R.success(setmalDtos);
    }
}
